<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet"
	href="${ctx }/js/ztree/css/zTreeStyle/zTreeStyle.css" />
<script type="text/javascript" src="${ctx }/js/bootstrap/jquery.js"></script>
<title>系统用户列表</title>
<script type="text/javascript">
	$(document).ready(function(){
		jQuery.fn.selectDept = function(targetId) {
			var _seft = this;
			var targetId = $(targetId);
		
			this.click(function(){
				var A_top = $(this).offset().top + $(this).outerHeight(true)-45;
				var A_left =  $(this).offset().left;
				targetId.show().css({"position":"absolute","top":A_top+"px" ,"left":A_left+"px"});
			});
		
			$(document).click(function(event){
				if(event.target.id!=_seft.selector.substring(1)){
					targetId.hide();	
				}
			});
		
			targetId.click(function(e){
				e.stopPropagation(); //  2
			});
		
		    return this;
		}
	
		$("#deptName").selectDept("#win-box");
	});

	var setting = {
		view: {
			showIcon: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: addOrEditMenu
		}
	};
		
	var zNodes = ${deptTree};

	var zTree;

	$(document).ready(function(){
		$.fn.zTree.init($("#deptTree"), setting, zNodes);
		zTree = $.fn.zTree.getZTreeObj("deptTree");
	});

	function addOrEditMenu(event, treeId, treeNode, clickFlag){
		$("#deptName").val(treeNode.name);
		$("#deptCode").val(treeNode.id);
		$("#win-box").hide();
	}


	/**
	 * 删除
	 */
	function deleteUser(id){
		art.dialog({
			content:"确认要删除该用户吗？",
			ok:function(){
				$.ajax({
					url:"${ctx}/user/delete",
					type:"POST",
					dataType:"text",
					data:"id="+id,
					success:function(msg){
						if(msg == "true"){
							art.dialog({content:"删除成功！",
								ok:function(){
									window.location.href="${ctx}/user/list";
								}
							});
						}else{
							art.dialog("删除失败！");
						}
					}
				});
			},
			cancel:true
		});
	}
	
	/**
	 * 启用/禁用
	 */
	function doED(status,id){
		var str = "";
		if(status == "1"){
			str = "启用";
		}else if(status == "0"){
			str = "禁用";
		}
		
		art.dialog({
			content:"确认要"+str+"该用户吗？",
			ok:function(){
				$.ajax({
					url:"${ctx}/user/doED",
					type:"POST",
					dataType:"text",
					data:"id="+id+"&status="+status,
					success:function(msg){
						if(msg == "true"){
							art.dialog({content:str+"成功！",
								ok:function(){
									window.location.href="${ctx}/user/list";
								}
							});
						}else{
							art.dialog(str+"失败！");
						}
					}
				});
			},
			cancel:true
		});
		
	}
	
	/**
	 * 重置密码
	 */
	function resetPwd(){
		var ids = getCheckedUserId();
		
		if(ids == ""){
			art.dialog("请选中用户！");
		}else{
			art.dialog({
				content:"确定要重置选中用户的密码吗？",
				ok:function(){
					$.ajax({
						url:"${ctx}/user/resetPwd",
						type:"POST",
						dataType:"text",
						data:"ids="+ids,
						success:function(msg){
							if(msg == "system error"){								
								art.dialog("重置失败！");
							}else{
								art.dialog({content:"重置密码成功，新密码为："+msg,
									ok:function(){
										window.location.href="${ctx}/user/list";
									}
								});
							}
						}
					});
				},
				cancel:true
			});
		}
		
	}
	
	function getCheckedUserId(){
		var idArray = [];
		
		$("input[type='checkbox']").each(function(){
			if($(this).is(":checked")){
				idArray.push(this.value);
			}
		});
		
		return idArray.join(",");
		
	}
</script>
</head>
<body>
	<div class="breadcrumbs" id="breadcrumbs">
		<script type="text/javascript">
        try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
    </script>

		<ul class="breadcrumb">
			<li><i class="ace-icon fa fa-home home-icon"></i> <a
				href="${ctx }/home/home">首页</a></li>
			<li class="active">系统管理</li>
			<li class="active">用户管理</li>
		</ul>
		<!-- .breadcrumb -->
	</div>

	<div class="page-content">
		<div class="page-header">
			<h1>
				用户管理 <small> <i class="ace-icon fa fa-double-angle-right"></i> 用户列表
				</small>
				<div style="float: right;">
					<button type="button"
						class="btn btn-primary btn-sm project-searchbutton">
						<i class="ace-icon fa fa-search"></i>展开查询
					</button>
					<shiro:hasPermission name="user:add">
						<button type="button" class="btn btn-success btn-sm"
							onclick="javascript:window.location.href='${ctx}/user/addInit'">
							<i class="ace-icon fa fa-plus"></i>新增用户
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="user:resetPassword">
						<button type="button" class="btn btn-default btn-sm"
							onclick="resetPwd()">
							<i class="ace-icon fa fa-repeat"></i>重置密码
						</button>
					</shiro:hasPermission>
				</div>
			</h1>
		</div>
		<!-- /.page-header -->

		<div class="row">
			<div class="col-xs-12 project-searchbox" style="display: none;">
				<div class="well col-xs-12">
					<form id="userForm" class="form-horizontal"
						action="${ctx }/user/list" method="post">
						<input type="hidden" id="pageNo" name="pageNo"
							value="${page.pageNo }" />
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>用户账号</strong></label>
								<div class="col-sm-8">
									<input type="text" id="userCode" name="userCode"
										value="${user.userCode }" class="form-control"
										placeholder="用户账号">
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>用户姓名</strong></label>
								<div class="col-sm-8">
									<input type="text" id="name" name="name" value="${user.name }"
										class="form-control" placeholder="用户姓名">
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>手机号码</strong></label>
								<div class="col-sm-8">
									<input type="text" id="mobile" name="mobile"
										value="${user.mobile }" class="form-control" placeholder="手机号码">
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>部门</strong></label>
								<div class="col-sm-8">
									<input type="text" id="deptName" name="department.name"
										value="${user.department.name }" class="form-control"
										readonly="readonly" placeholder="选择部门" /> <input type="hidden"
										id="deptCode" name="department.id"
										value="${user.department.id }" />
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>状态</strong></label>
								<div class="col-sm-8">
									<select:option type="status" id="status" name="status"
										styleClass="form-control" blank="true" showValue="false"
										value="${user.status }" />
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4">
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>性别</strong></label>
								<div class="col-sm-8">
									<select:option type="sex" id="sex" name="sex"
										styleClass="form-control" blank="true" showValue="false"
										value="${user.sex }" />
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-md-6 col-lg-4" style="float: right;">
							<div class="col-sm-8 col-sm-offset-4 project-nopadding"
								style="text-align: right;">
								<button type="button" class="btn btn-primary btn-sm"
									onclick="doSubmit('userForm')">
									<i class="ace-icon fa fa-search"></i> 查询
								</button>
								<button type="button" class="btn btn-default btn-sm"
									onclick="doFormReset('userForm')">
									<i class="ace-icon fa fa-undo"></i>重置
								</button>
							</div>
						</div>
					</form>
				</div>
			</div>
			<div class="col-xs-12">
				<!-- <div class="space-10"></div> -->
				<div class="table-responsive">
					<display:table class="project-default project-colorful"
						name="page.result" style="width:100%;border:0;" id="user">
						<display:column title="选择" style="width:5%">
							<input type="checkbox" id="sed_${user.id }" value="${user.id }" />
						</display:column>
						<display:column title="用户账号" style="width:10%">
							<a class="blue" href="${ctx }/user/view?id=<c:out value='${user.id}'/>">
								<c:out value="${user.userCode }"/> </a>
						</display:column>
						<display:column title="用户姓名" style="width:10%">
							<c:out value="${user.name }"/>
						</display:column>
						<display:column title="性别" style="width:5%">
							<select:write type="sex" value="${user.sex }" />
						</display:column>
						<display:column property="mobile" title="手机号码" style="width:10%" />
						<display:column property="createUser" title="创建人"
							style="width:10%" />
						<display:column title="创建时间 " style="width:15%">
							<fmt:formatDate value="${user.createTime }"
								pattern="yyyy-MM-dd HH:mm:ss" />
						</display:column>
						<display:column property="department.name" title="部门"
							style="width:10%" />
						<display:column title="状态" style="width:10%">
							<select:writeClass type="status" value="${user.status }" />
						</display:column>
						<display:column title="操作" style="width:10%">
<%-- 							<c:if test="${user.  != systemCode }"> --%>
<%--  								<a class="blue" href="${ctx }/user/view?id=${user.id}" title="查看"> --%>
<!--             					<i class="ace-icon fa fa-file-text bigger-130"></i> -->
<!--             				</a> -->
<!--             				&nbsp; -->
							<c:if test="${user.userCode  == systemCode }">
								<shiro:hasPermission name="user:update">
									<a class="green" href="${ctx }/user/updateInit?id=${user.id}"
										title="修改"> <i class="ace-icon fa fa-edit bigger-130"></i>
									</a>
								</shiro:hasPermission>
							</c:if>
            				<c:if test="${user.userCode  != systemCode }">
								<shiro:hasPermission name="user:update">
									<a class="green" href="${ctx }/user/updateInit?id=${user.id}"
										title="修改"> <i class="ace-icon fa fa-edit bigger-130"></i>
									</a>
								</shiro:hasPermission>
								<shiro:hasPermission name="user:delete">
	            			&nbsp;
            				<a class="grey" href="javascript:void(0);"
										onclick="deleteUser('${user.id}')" title="删除"> <i
										class="ace-icon fa fa-trash bigger-130"></i>
									</a>
								</shiro:hasPermission>
								<shiro:hasPermission name="user:erduser">
            				&nbsp;
            				<c:if test="${user.status == '1' }">
										<a class="red" href="javascript:void(0);"
											onclick="doED('0','${user.id}')" title="禁用"> <i
											class="ace-icon fa fa-lock bigger-130"></i>
										</a>
									</c:if>
									<c:if test="${user.status == '0' }">
										<a class="green" href="javascript:void(0);"
											onclick="doED('1','${user.id}')" title="启用"> <i
											class="ace-icon fa fa-unlock bigger-130"></i>
										</a>
									</c:if>
								</shiro:hasPermission>
 							</c:if> 

						</display:column>

					</display:table>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="space-10"></div>
				<div class="project-pagebox">
					<div>
						<p:writerPage pageNo="${page.pageNo }"
							pageSize="${page.pageSize }" totalCount="${page.totalCount }"
							formId="userForm" />
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="col-xs-3" style="display: none; padding-left: 0px;"
		id="win-box">
		<div class="widget-box" style="margin-top: 0px;">
			<div class="widget-header header-color-blue2">
				<h4 class="lighter smaller">选择部门</h4>
			</div>
			<div class="widget-body">
				<div class="widget-main padding-8">
					<ul id="deptTree" class="ztree"></ul>
				</div>
			</div>
		</div>
	</div>
</body>
</html>